<?php
/**
 * 万年历
 */
$week = array();
$week[0] = '日';
$week[1] = '一';
$week[2] = '二';
$week[3] = '三';
$week[4] = '四';
$week[5] = '五';
$week[6] = '六';

$reply = '今天是 ' . date('Y年n月j日') . ' 星期' . $week[date('w')];

$url = 'https://hl.zdic.net/j/gl/' . date('Y/n') . '.php';
$response = curl_http_request($url);
// 调试日志
//file_put_contents(runtime_path() . '/userApi.log', PP($response, false));
if (!empty($response['response'])) {
    if (preg_match('#\((.*)\)#', $response['response'], $content)) {
        $data = json_decode($content[1], true);
        //file_put_contents(runtime_path() . '/userApi.log', PP($data, false));
        if (is_array($data)) {
            $today = date('Y-m-d');
            foreach ($data as $item) {
                if ($item['date'] === $today) {
                    // 农历
                    if (!empty($item['nongliDate']) && !empty($item['nongliDay'])) {
                        $reply .= "\n==================";
                        $reply .= PHP_EOL . $item['nongliDate'] . ' ' . $item['nongliDay'];
                    }
                    // 星座
                    if (!empty($item['xingzuo'])) {
                        $reply .= PHP_EOL . "星座: " . $item['xingzuo'];
                    }
                    // 历史上的今天
                    if (!empty($item['history'])) {
                        $reply .= "\n==================\n";
                        $reply .= "历史上的今天\n";
                        $i = 1;
                        foreach ($item['history'] as $row) {
                            $reply .= $i . ": " . $row['title'] . PHP_EOL;
                            $i++;
                        }
                    }
                    break;
                }
            }
        }
    }
}

echo $reply;
